<template>
  <div class="shopcar-container">
    <!-- 商品列表区域 -->
    <div class="mui-card" v-for="item in shopcarList" :key="item.id">
      <div class="mui-card-content">
        <div class="mui-card-content-inner goods-item">
          <mt-switch
            v-model="selectedObj[item.id]"
            @change="changeSelectState(item.id)"
          ></mt-switch>
          <img :src="item.thumb_path" alt="" />
          <div class="info">
            <h1 class="title">{{ item.title }}</h1>
            <div class="goods-info">
              <span class="price">¥{{ item.sell_price }}</span>
              <nobox :initcount="countObj[item.id]" :id="item.id"></nobox>
              <a href="#" @click.prevent="del(item.id)">删除</a>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 结算区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner sum-up">
          <div class="sum-info">
            <p>总计（不含运费）</p>
            <p>
              已勾选商品<span class="danger">{{ selectedCount }}</span
              >件，总价<span class="danger">¥{{ selectedAmount }}</span>
            </p>
          </div>
          <mt-button type="danger">去结算</mt-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from "vuex";
import nobox from "../sub-components/Shopcar_NoBox.vue";
export default {
  data() {
    return {
      shopcarList: [], // 通过api请求获取的购物车商品信息数组
    };
  },
  created() {
    this.getShopcarListByIds();
  },
  methods: {
    ...mapMutations(["delFromCart", "changeSelectState"]),
    async getShopcarListByIds() {
      if (this.idStr.length <= 0) return;
      const { data } = await this.$http.get(
        "api/goods/getshopcarlist/" + this.idStr
      );
      if (data.status === 0) {
        this.shopcarList = data.message;
      }
    },
    del(id) {
      this.shopcarList.some((item, i) => {
        if (item.id === id) {
          this.shopcarList.splice(i, 1);
          return true;
        }
      });
      // 从vuex中删除数据
      this.delFromCart(id);
    },
  },
  computed: {
    ...mapGetters([
      "idStr",
      "countObj",
      "selectedObj",
      "selectedCount",
      "selectedAmount",
    ]),
  },
  components: {
    nobox,
  },
};
</script>

<style lang="scss" scoped>
.shopcar-container {
  overflow: hidden;
  background-color: #efeff4;
  .goods-item {
    display: flex;
    // justify-content: space-between;
    img {
      width: 60px;
      height: 60px;
    }
    .info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .title {
        font-size: 14px;
      }

      .goods-info {
        display: flex;
        justify-content: space-between;
        align-items: center;
        .price {
          color: red;
          font-weight: bold;
          font-size: 16px;
        }
      }
    }
  }

  .sum-up {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .sum-info {
      .danger {
        color: red;
        font-weight: bold;
        font-size: 16px;
      }
    }
  }
}
</style>